﻿using System;
using System.Collections.Generic;
using System.Linq;
using SimpleBlog.Models;
using SimpleBlog.Infrastructure.Repositories;
using SimpleBlog.Infrastructure.Database;

namespace SimpleBlog.Infrastructure.Repositories
{
    public class PostRepository : Repository<Post>, IPostRepository
    {
        protected override IQueryable<Post> DefaultSet
        {
            get
            {
                return _Context.Posts.Include("Creator").Include("Tags").Include("Comments");
            }
        }

        protected override Func<Post, object> DefaultOrderBy
        {
            get
            {
                return x => x.Created;
            }
        }

        public PostRepository(SbContext context) : base(context) { }

        public override Post FindById(int id)
        {
            return (from entity in DefaultSet
                    where entity.PostId == id
                    select entity).SingleOrDefault();
        }

        public override IEnumerable<Post> FindById(params int[] ids)
        {
            return from entity in DefaultSet
                   where ids.Contains(entity.PostId)
                   select entity;
        }
    }
}